package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.model.Goods;
import com.rongke.model.NewGoods;
import com.rongke.service.GoodsService;
import com.rongke.service.NewGoodsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @NewGoodsController
 * @新品Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/newGoods")
@Transactional
@CrossOrigin
public class NewGoodsController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private NewGoodsService newGoodsService;
    @Autowired
    private GoodsService goodsService;

    /**
     * @添加新品首发
     * @param newGoods
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addExperienceGoods(@RequestBody List<NewGoods> newGoods ){
        log.debug("添加新品商品");
        newGoodsService.insertBatch(newGoods);
        return JsonResp.ok("");
    }
    /**
     * @修改新品
     * @param newGoods
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateNewGoods(@RequestBody NewGoods newGoods){
        log.debug("修改新品");
        newGoods.setUptDatetime(new Date());
        newGoodsService.updateById(newGoods);
        return JsonResp.ok(newGoods);
    }
    /**
     * @根据id查找新品
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectNewGoods(Long id){
        log.debug("查找新品");
        NewGoods newGoods = newGoodsService.selectById(id);
        return JsonResp.ok(newGoods);
    }

    /**
     * @根据条件查找新品
     * @param newGoods
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectNewGoodsByCondition(@RequestBody NewGoods newGoods){
        log.debug("根据条件查找新品");
        if(newGoods==null)
            return JsonResp.toFail("参数不能为空");
        List<NewGoods> newGoodsList=newGoodsService.selectByCondition(newGoods);
        return JsonResp.ok(newGoodsList);
    }

    /**
     * @分页查找新品首发商品
     * @param page
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectPage", method = RequestMethod.GET)
    public JsonResp selectNewGoods(Page page,Integer type){
        log.debug("分页查找新品首发商品\n");
        if(type==null){
            return JsonResp.toFail("参数不能为空");
        }
        Map map=new HashMap();
        Integer startIndex=(page.getCurrent()-1)*page.getSize();
        map.put("startIndex",startIndex);
        map.put("size",page.getSize());
        map.put("type",type);
        List<NewGoods> goodsPage = newGoodsService.selectExpGoodsList(map);
        if(!goodsPage.isEmpty()){
            goodsPage.forEach(e->{
                goodsService.fetchGoodsInventoryStatus(e.getGoods());//判断库存状态
            });
        }
        int total=newGoodsService.selectCount(null);
        page.setRecords(goodsPage);
        page.setTotal(total);
        return JsonResp.ok(page);
    }

    /**
     * 获取某产品线下已发布未分组的商品集合（交给web客户端分页）
     * @param goodsType
     * @return
     */
    @RequestMapping(value = "/getNotNewGoodsList",method = RequestMethod.GET)
    public JsonResp getNotGroupedGoodsList(int goodsType){
        log.debug("获取某产品线下未分组的商品集合");
        EntityWrapper<Goods> ew=new EntityWrapper<>();
        //goods_status 商品状态 1代表只上传未发布 2代表正式发布上架 3 下架
        ew.eq("goods_status",2).eq("goods_type",goodsType);
        List<Goods> goodsList=goodsService.selectList(ew);
        //查询已有的商品
        List<NewGoods> popGoodses=newGoodsService.selectExpGoodsList(new HashMap());
        if(!popGoodses.isEmpty()){
            for(int i=0,y=popGoodses.size();i<y;i++){
                Goods goods=popGoodses.get(i).getGoods();
                if(goodsList.contains(goods)){
                    goodsList.remove(goods);
                }
            }
        }

        return JsonResp.ok(goodsList);
    }
    /**
     * @删除
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/deleteNewGoods", method = RequestMethod.GET)
    public JsonResp deleteExGoods(Long id){
        log.debug("删除");
        newGoodsService.deleteById(id);
        return JsonResp.ok();
    }

    /**
     * @设置和取消首页显示
     * @param id
     * @param isHome
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/updIsHome", method = RequestMethod.GET)
    public JsonResp updIsHome(Long id,Integer isHome){
        log.debug("设置和取消首页显示");
        NewGoods ng = newGoodsService.selectById(id);
            ng.setIsHome(isHome);
            newGoodsService.updateById(ng);
            return JsonResp.ok();
    }

}
